package Server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimerTask;
import Controllers.MessageController;
import MessagePacks.OneProcessPack;
import entities.NotificationMessage;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

	/**
	 * This class will hold a scheduled task.
	 */
	public class SchedTask extends TimerTask{
		
		private int taskType;
		private Scheduler scheduler;

		/**
		 * Constructor for SchedTask class.
		 * This class will self-call at pre-determined time
		 * @param task - Task to perform on wake up
		 */

		public SchedTask(int task, Scheduler scheduler) {
			this.taskType = task;
			this.scheduler = scheduler;
		}

		/**
		 * Will call this function upon wake up
		 */
		public void run(){

			switch(taskType){
				
			case Scheduler.DAILY_TASKS:{
				DailyTasks();
				break;
			}
			case Scheduler.WEEKLY_TASKS:{ //Do we Need Weekly Task?
				//WeeklyTasks();
				break;
			}
			
			}
			
		}
		
		
		public void DailyTasks(){
			//BookController bookController = new BookController(scheduler.server, scheduler.DB); //Process Controller Instead

			//Create Message Controller
			MessageController msgcon = new MessageController(scheduler.getSqlDB());
			
			Calendar today = Calendar.getInstance(); //Create Calendar Instance
			today.add(Calendar.DAY_OF_MONTH, 1);   //Move To The Next Day
			String currDate = today.get(Calendar.YEAR) + "-"; //String With The Current Year And "-"
			if (today.get(Calendar.MONTH) < 10) currDate += "0" + (today.get(Calendar.MONTH) + 1) + "-"; //Add To String The Current Month And "-", if The Month is a marked by one figure add 0 before
			else currDate += (today.get(Calendar.MONTH) + 1) + "-";//Add To String The Current Month And "-"
			if (today.get(Calendar.DAY_OF_MONTH) < 10) currDate += "0" + today.get(Calendar.DAY_OF_MONTH);//Add To String The Current Day, if The Month is a marked by one figure add 0 before
			else currDate += today.get(Calendar.DAY_OF_MONTH);//Add To String The Current Month
			//Finally - currDate holds the current date (after it "moved" to tomorrow)
			
			//Example For Date Query
			//String query = "SELECT * FROM loans WHERE RETURN_DATE = '" + currDate +"';"; //Need To Change Query
			
			//Get All The Processes That Are Not Ended/Terminated/Suspended
			ArrayList<String> mMap = new ArrayList<String>();
			ResultSet rs = scheduler.getSqlDB().GetAllActiveProcess(); //Activate Query From DB
			try
			{
			while(rs.next())
				{
				mMap.add(rs.getString(1));
				}
			} catch (SQLException e) {
			// TODO handle this error
			}
			
			//Update Time In Active Processes
			for(int i=0;i<(mMap.size());i++)
				{
				scheduler.getSqlDB().DayEndedSoChangeTimeOfProcess(Integer.parseInt(mMap.get(i)));
				}
						
			String UserMail = new String("gioyaf@gmail.com");
			String PhaseManagerMail = new String("nirporat@gmail.com");
			String SupervisorMail = new String("gritzman@outlook.com");
			String DepManagerMail = new String("alapinh@gmail.com");
			
			//Send Message To Appropriate 
			for(int i=0;i<(mMap.size());i++)
				{
				//get IsDepManager
				int depManagerID;
				depManagerID = scheduler.getSqlDB().getIsDepManager();
				//get the process supervisor ID and current phase manager ID
				OneProcessPack prcPck = new OneProcessPack(Integer.parseInt(mMap.get(i)));
				try {
					scheduler.getSqlDB().getAllDataFromProc(prcPck);
				} catch (SQLException e) {
					e.printStackTrace();
				}
				int curPhase = prcPck.getCurrentPhase(); //Current Phase Of The Process
				int supervisorID=prcPck.getSupervisorID(); //Process SupervisorId
				int phaseManagerID; //Will Hold The Current Phase Manager
				switch(curPhase){
								case 1:
									phaseManagerID=prcPck.getEstimatorID();
									break;
								case 2:
									phaseManagerID=prcPck.getChairmanID();
									break;	
								case 3:
									phaseManagerID=prcPck.getExecLeaderID();
									break;	
								case 4:
									phaseManagerID=prcPck.getTesterID();
									break;	
									default:phaseManagerID=prcPck.getSupervisorID();
								}
				
				int timeLeft = scheduler.getSqlDB().getTimeLeftForProcess(Integer.parseInt(mMap.get(i)));
				if(timeLeft!=-1000) //Sign For SQL Failure
				switch(timeLeft)
						{
						case 1:{
							NotificationMessage nMsg = new NotificationMessage(0,phaseManagerID,Integer.parseInt(mMap.get(i)),NotificationMessage.Warning,0);
							msgcon.storemsg(nMsg);
							//msgcon.SendMail(PhaseManagerMail,nMsg.getmsg(),"DeadLine Warning");
							}
							break;
						case 0:{
							NotificationMessage nMsg = new NotificationMessage(0,phaseManagerID,Integer.parseInt(mMap.get(i)),NotificationMessage.Exception4PhaseManaer,0);
							msgcon.storemsg(nMsg);
							//msgcon.SendMail(PhaseManagerMail,nMsg.getmsg(),"Time Exception");
							nMsg = new NotificationMessage(0,supervisorID,Integer.parseInt(mMap.get(i)),NotificationMessage.Exception4Supervisor,0);
							msgcon.storemsg(nMsg);
							//msgcon.SendMail(SupervisorMail,nMsg.getmsg(),"Time Exception");
							nMsg = new NotificationMessage(0,depManagerID,Integer.parseInt(mMap.get(i)),NotificationMessage.Exception4DepManager,0);
							msgcon.storemsg(nMsg);
							//msgcon.SendMail(DepManagerMail,nMsg.getmsg(),"Time Exception");
							}
							break;
						}
				}
			
		}
		
		/*public void WeeklyTasks(){
			ReportController reportController = new ReportController(scheduler.server, scheduler.DB);
			reportController.BuildActivityReports();
			
		}*/

}
